const dbConfig = require("../../../utils/dbConfig");
const jwToken = require('jsonwebtoken')


// 登录接口
const login = async (req, res) => {
  let { username, password } = req.query;
  let token = jwToken.sign({ username: username }, process.env.JWT_SECRET)
  let sql = "select * from user where phone=? and password=? or username=? and password=? and role='admin'"
  let sqlArr = [username, password, username, password]
  try {
    const result = await dbConfig.SySqlConnect(sql, sqlArr)
    if(result){
      res.send({
        code: 200,
        data: token,
        msg: "登录成功",
      })
    }else{
      res.send({
        code: 400,
        tip: "用户名或者密码错误",
        data: ''
      })
    }
  } catch (e) {
    console.error(e)
    res.status(500).send('Something broke!')
  }
}


module.exports = {
  login
};